package com.sweets.look.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import com.sweets.look.R;
import com.sweets.look.util.ViewUtils;

public class MarkSheetMenu {
    public interface OnSheetItemClickListener {
        void onClick(int arg1);
    }

    public class SheetItem {
        final MarkSheetMenu.OnSheetItemClickListener itemClickListener;
        final String name;

        public SheetItem(String name, OnSheetItemClickListener itemClickListener) {
            super();
            this.name = name;
            this.itemClickListener = itemClickListener;
        }
    }

    private final Context context;
    private PopupWindow dialog;
    private LinearLayout lLayout_content;
    private int mMenuHeight;
    private List<SheetItem> sheetItemList;
    private View view;

    public MarkSheetMenu(Context context) {
        super();
        this.mMenuHeight = 0;
        this.context = context;
    }


    public MarkSheetMenu addSheetItem(String strItem, OnSheetItemClickListener listener) {
        if (this.sheetItemList == null) {
            this.sheetItemList = new ArrayList<>();
        }

        this.sheetItemList.add(new SheetItem(strItem, listener));
        return this;
    }

    public MarkSheetMenu builder() {
        this.dialog = new PopupWindow(this.context);
        this.dialog.setWidth(-2);
        this.dialog.setHeight(-2);
        this.view = LayoutInflater.from(this.context).inflate(R.layout.widget_sheet_menu,
                (ViewGroup) ((Activity) this.context).findViewById(android.R.id.content), false);//16908290
        this.lLayout_content = (LinearLayout) this.view.findViewById(R.id.lLayout_content);
        return this;
    }

    public MarkSheetMenu setFocusable(boolean cancel) {
        this.dialog.setFocusable(cancel);
        return this;
    }

    public MarkSheetMenu setOutsideTouchable(boolean cancel) {
        this.dialog.setOutsideTouchable(cancel);
        return this;
    }

    private void setSheetItems() {
        if (this.sheetItemList != null && this.sheetItemList.size() > 0) {
            int size = this.sheetItemList.size();
            for (int i = 1; i <= size; i++) {
                SheetItem v4 = this.sheetItemList.get(i - 1);
                String v6 = v4.name;
                final OnSheetItemClickListener listener = v4.itemClickListener;
                TextView textView = new TextView(this.context);
                textView.setText(v6);
                textView.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
                textView.setTextSize(2, 15f);
                textView.setGravity(3);
                textView.setTextColor(Color.parseColor("#f5f5f5"));
                textView.setBackgroundResource(R.drawable.menu_btn_bg);
                textView.setClickable(true);
                textView.setPadding(ViewUtils.convertToDensityPixels(this.context, 15), ViewUtils.convertToDensityPixels(
                        this.context, 8), ViewUtils.convertToDensityPixels(this.context, 15), ViewUtils
                        .convertToDensityPixels(this.context, 8));
                final int index = i;
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.onClick(index);
                        MarkSheetMenu.this.dialog.dismiss();
                    }
                });
                textView.measure(View.MeasureSpec.makeMeasureSpec(
                        ViewUtils.getWindowWidth(this.context), View.MeasureSpec.AT_MOST),//-2147483648
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));//0 0
                this.mMenuHeight += textView.getMeasuredHeight();
                this.lLayout_content.addView(textView);
            }
        }
    }

    public void show(View v, int x, int y) {
        this.setSheetItems();
        this.dialog.setContentView(this.view);
        this.dialog.setAnimationStyle(R.style.DialogAnimation);
        this.dialog.update();
        this.dialog.setBackgroundDrawable(this.context.getResources().getDrawable(R.drawable.menu_bg));
        if (ViewUtils.getWindowHeight(this.context) > y * 2) {
            this.dialog.showAsDropDown(v, x, y);
        } else {
            int v0 = this.sheetItemList.size();
            this.dialog.showAtLocation(v, 0, x, y - this.mMenuHeight / v0 * (v0 - 2));
        }
    }
}

